package top.go2do.homeGoods.business.goods.controller;

import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.go2do.homeGoods.business.goods.module.*;
import top.go2do.common.annotation.UserInfoFill;
import top.go2do.common.controller.TomatoController;
import top.go2do.common.module.UserInfoBO;
import top.go2do.common.module.pageQuery.PageResult;
import top.go2do.homeGoods.business.goods.service.GoodsService;

import java.util.List;

/**
 * @Description
 * @Author xingyj
 * @CreateTime 2025/6/18 13:01
 */
@Slf4j
@RequestMapping("/goods")
@RestController
public class GoodsController implements TomatoController<GoodsPO, GoodsCreateForm, GoodsModifyForm, Long, GoodsQueryCondition, GoodsQueryAllCondition, GoodsListVO> {

    @Autowired
    private GoodsService goodsService;

    @Override
    public GoodsPO create(GoodsCreateForm createForm, UserInfoBO userInfo) {
        log.info("goodss create appId:{}, goodsName:{}", userInfo.getAppId(), createForm.getGoodsName());
        createForm.setAppId(userInfo.getAppId());
        createForm.setUserId(userInfo.getUserId());
        return goodsService.create(createForm);
    }

    @Override
    public void modifyById(Long id, GoodsModifyForm modifyForm, UserInfoBO userInfo) {
        modifyForm.setGoodsId(id);
        modifyForm.setUserId(userInfo.getUserId());
        goodsService.modifyById(modifyForm);
    }

    @Override
    public void removeById(Long id, UserInfoBO userInfo) {
        goodsService.removeById(id);
    }

    @Override
    public GoodsPO getById(Long id) {
        return goodsService.getById(id);
    }

    @Override
    public List<GoodsListVO> listAll(GoodsQueryAllCondition condition) {
        List<GoodsPO> poList = goodsService.listAll(condition);
        List<GoodsListVO> volist = goodsService.po2vo(poList);
        return volist;
    }

    @Override
    public PageResult<GoodsListVO> pageQuery(GoodsQueryCondition goodsQueryCondition) {
        PageResult<GoodsPO> pageResult = goodsService.pageQuery(goodsQueryCondition);
        List<GoodsListVO> volist = goodsService.po2vo(pageResult.getList());
        PageResult<GoodsListVO> finalResult = new PageResult<>();
        BeanUtils.copyProperties(pageResult, finalResult);
        finalResult.setList(volist);
        return finalResult;
    }

    @PatchMapping("/setPerms")
    public void setTags(@Valid @RequestBody GoodsTagsForm goodsTagsForm, @UserInfoFill UserInfoBO userInfo) {
        goodsTagsForm.setUserId(userInfo.getUserId());
        goodsService.setTags(goodsTagsForm);
    }
}
